package lib

import (
	"fmt"
	"io/ioutil"
	"regexp"
)

func GetVideoByType(msg string) (videoURL string) {
	arr := []string{"网红", "明星", "热舞", "风景", "游戏", "动物"}
	if !inArray(msg, arr) {
		return
	}
	resp, err := HttpGet(fmt.Sprintf("http://shengapi.cn/api/jingxuanshipin.php?type=%s", msg))
	if err != nil {
		return
	}
	if resp.StatusCode != 200 {
		return
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	re := regexp.MustCompile(`播放链接：(https?://(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*)`)
	res := re.FindSubmatch(body)
	if len(res) > 2 {
		for i, v := range res {
			if i == 1 {
				videoURL = string(v)
				return
			}
		}
	}
	return
}

func inArray(str string, arr []string) (b bool) {
	b = false
	for _, v := range arr {
		if str == v {
			b = true
			return
		}
	}
	return
}
